﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Xml;
using System.Text;

namespace CAMLViewer.Layouts.CAMLViewer
{
    public partial class CAMLViewer : LayoutsPageBase
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnGo_click(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            lstViews.Items.Clear();
            lstLists.Items.Clear();
            txtQuery.Text = "";
            
            
            dvInternalNames.InnerHtml = "";
            trViewID.Visible = false;
            trListID.Visible = false;
            trQuery.Visible = false;
            trInternalNames.Visible = false;

            using (ClientContext clientContext = new ClientContext(TextBox1.Text))
            {
                Web site = clientContext.Web;
                ListCollection listColl = site.Lists;
                clientContext.Load(listColl,
                    lists => lists.Include(
                        list => list.Title
                        ));

                try
                {
                    clientContext.ExecuteQuery();
                    foreach (List list in listColl)
                    {
                        lstLists.Items.Add(list.Title);
                    }
                }
                catch (Exception ex)
                {

                    lblStatus.Text = "Error : " + ex.Message;
                }
             
            }
        
        }
        protected void lstLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            trListID.Visible = true;
            trInternalNames.Visible = true;
            trQuery.Visible = false;
            trViewID.Visible = false;

            lblStatus.Text = "";
            lstViews.Items.Clear();
            
            using (ClientContext clientContext = new ClientContext(TextBox1.Text))
            {
                clientContext.Load(clientContext.Web);
                List list = clientContext.Web.Lists.GetByTitle(Convert.ToString(lstLists.SelectedItem));
                clientContext.Load(list,
                   l => l.Id,
                   l => l.Views,
                   l => l.Fields);
                try
                {
                    clientContext.ExecuteQuery();
                    lblListID.Text = Convert.ToString(list.Id);
                    lstViews.Items.Clear();
                    foreach (Microsoft.SharePoint.Client.View v in list.Views)
                    {
                        lstViews.Items.Add(v.Title);

                    }
                    
                    dvInternalNames.InnerHtml = "<table width='100%'>";
                    dvInternalNames.InnerHtml = "<tr><th>Field</th><th>Internal Name</th></tr>";
                    foreach (Field f in list.Fields)
                    {
                        dvInternalNames.InnerHtml += "<tr><td>" + f.Title + "</td><td>" + f.InternalName + "</td></tr>";
                    }
                    dvInternalNames.InnerHtml += "</table>";
                }
                catch (Exception ex)
                {
                    lblStatus.Text = "Error : " + ex.Message;
                } 
            }
        }
        protected void lstViews_SelectedIndexChanged(object sender, EventArgs e)
        {
            trViewID.Visible = true;
            trQuery.Visible = true;
            lblStatus.Text = "";
            using (ClientContext clientContext = new ClientContext(TextBox1.Text))
            {
                List list = clientContext.Web.Lists.GetByTitle(lstLists.SelectedItem.ToString()); 
                Microsoft.SharePoint.Client.View view = list.Views.GetByTitle(lstViews.SelectedItem.ToString());
                
                clientContext.Load(view,
                    v => v.ViewQuery,
                    v => v.Id);
                try
                {
                    clientContext.ExecuteQuery();
                    //Populate Query
                    string Query = IndentXMLString("<Query xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" + view.ViewQuery + "</Query>");
                    txtQuery.Visible = true;
                    txtQuery.Text = Query;

                    //Populate Fields and Internal Names
                    lblViewId.Text = Convert.ToString(view.Id);
                }
                catch (Exception ex)
                {
                    lblStatus.Text = "Error : " + ex.Message;
                }
            }
 
        }

        private static string IndentXMLString(string xml)
        {
            //Thanks : http://www.knowdotnet.com/articles/indentxml.html
            //string outXML;
            MemoryStream ms = new MemoryStream();
            XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode);
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(xml);
                xtw.Formatting = Formatting.Indented;
                doc.WriteContentTo(xtw);
                xtw.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                StreamReader sr = new StreamReader(ms);
                return sr.ReadToEnd();

            }
            catch
            {
                return string.Empty;
            }
            
        
        }
    }
}
